using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using MM.Entities;

namespace MM.Business
{
    class clsQUYEN
    {
        public clsQUYEN(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<QUYEN> DataTableToLstObj(DataTable dt)
        {
            QUYEN quyenDTO;
            List<QUYEN> lstQuyenDTO = new List<QUYEN>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                quyenDTO = new QUYEN();
                try
                {
                    quyenDTO._moTa = dt.Rows[i]["MoTa"].ToString();
                }
                catch 
                {
                }
                try
                {
                    quyenDTO._tenForm = dt.Rows[i]["TenForm"].ToString();
                }
                catch 
                {
                }
                lstQuyenDTO.Add(quyenDTO);
            }
            return lstQuyenDTO;
        }
        public QUYEN DataTableToObj(DataTable dt)
        {
            QUYEN quyenDTO = new QUYEN();
            try
            {
                quyenDTO._moTa = dt.Rows[0]["MoTa"].ToString();
            }
            catch 
            {
            }
            try
            {
                quyenDTO._tenForm = dt.Rows[0]["TenForm"].ToString();
            }
            catch 
            {
            }
            return quyenDTO;
        }
        public List<QUYEN> LoadPhanQuyenCu(int maCD)
        {
            DataTable dt = new DataTable();
            List<QUYEN> lstQuyenDTO = new List<QUYEN>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "sp_LoadPhanQuyenCu";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChucDanh", maCD);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstQuyenDTO = DataTableToLstObj(dt);
            }
            catch 
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstQuyenDTO;
        }

        public List<QUYEN> LoadQuyenByChucDanh(int maCD)
        {
            DataTable dt = new DataTable();
            List<QUYEN> lstQuyenDTO = new List<QUYEN>();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "sp_LoadQuyenByChucDanh";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@MaChucDanh", maCD);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstQuyenDTO = DataTableToLstObj(dt);
            }
            catch 
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstQuyenDTO;
        }
    }
}
